<template>
	<view class="">
		<u-form-item :label="item.label" borderBottom :prop="item.value" :required="item.required">
			<u-upload :disabled="disabled" ref="formitem" :fileList="fileList" @afterRead="afterRead"
				:maxCount="maxCount" :maxSize="maxSize" :accept="accept" @delete="deletePic"
				:uploadIcon="uploadIcon"></u-upload>
			<!-- @afterRead="afterRead"
      @delete="deletePic" -->
		</u-form-item>
	</view>
</template>

<script>
	import upload from "@/utils/upload.js";
	import commonConfig from "@/config";
	export default {
		mixins: [uni.$u.mpMixin, uni.$u.mixin],
		props: {
			item: {
				typeof: Object,
				default: () => {
					return {};
				},
			},
			data: {
				typeof: Object,
				default: () => {
					return {};
				},
			},
			value: {
				typeof: [String, Number, Boolean, Array, Object],
				default: () => {
					return "";
				},
			},
		},
		data() {
			return {
				// inputValue: "",
				show: false,
				//   fileList: [],
			};
		},
		computed: {
			disabled() {
				this.getParentData("u-form")
				return this.parent.disabled;
			},

			accept() {
				return this.item?.attrs?.accept || "image";
			},
			uploadIcon() {
				return this.item?.attrs?.uploadIcon || "camera-fill";
			},
			maxCount() {
				return this.item?.attrs?.maxCount || 1;
			},
			maxSize() {
				return this.item?.attrs?.maxSize || Number.MAX_VALUE;
			},
			fileList: {
				get() {
					if (!this.value || this.value == "") {
						return [];
					}
					return this.value.split(",").map((e) => {
						return {
							url: e,
						};
					});
				},
				set(val) {
					let v = val.map((e) => e.url).join(",");
					this.$emit("input", v);
					uni.$u.formValidate(this.$refs.formitem, "blur");
				},
			},
		},
		methods: {
			deletePic(event) {
				this.fileList.splice(event.index, 1);
				this.$emit("input", this.fileList.map((e) => e.url).join(","));

				uni.$u.formValidate(this.$refs.formitem, "blur");
			},
			blur() {
				this.$emit("input", this.inputValue);
			},
			select(e) {
				this.$emit("input", String(e.value));
				this.show = false;
			},
			afterRead(file) {
				upload({
					url: commonConfig.baseApi + "/app-api/infra/file/upload",
					filePath: file.file.url,
				}).then((e) => {
					this.fileList = [...this.fileList, {
						url: e.data
					}];
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	.uni-input-input {
		font-size: 14px !important;
		color: #171a1d !important;
		line-height: 99rpx !important;
		height: 99rpx !important;
		padding-left: 10px;
	}

	.uni-forms-item__label {
		margin-left: 0;
		font-size: 14px !important;
		color: #171a1d !important;
		line-height: 99rpx !important;
		height: 99rpx !important;
	}
</style>